package eventloop;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.apache.log4j.Logger;
import rtmp.RtmpResponse;
import user.Publish;
import user.PublishGroup;
import user.Receive;
import user.ReceiveGroup;
import util.Common;
import util.MsgType;

import java.util.List;

import static util.Common.FLV_KEY_FRAME;

public class VideoRunnable implements Runnable{

    private final static Logger logger = Logger.getLogger(VideoRunnable.class);

    private byte[] message;
    private int timestamp;
    private Publish publish;
    private List<Receive> listVideo;
    public VideoRunnable(byte[] message, Publish publish, List<Receive> listVideo, int timestamp) {
        this.message = message;
        this.publish = publish;
        this.timestamp = timestamp;
        this.listVideo = listVideo;
    }

    @Override
    public void run() {
        try{
            byte flags = message[0];
            if (publish != null && !publish.keyFrame) { //推流端进来 第一帧就为 关键帧，其实需要加上判断
                publish.keyFrame = true;
                publish.keyFrameMessage = message;
            }
            if (listVideo != null) {
                for (Receive receive : listVideo) {
                    if (!receive.playing) {
                        continue;
                    }
                    if (receive != null && receive.playing) {
                        if (flags >> 4 == FLV_KEY_FRAME && !receive.ready) { //获取到了推流端的关键帧
                            logger.error("flags ====== " + flags);
                            ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(1024);
                            byteBuf.writeByte(0x00);
                            byteBuf.writeByte(0x00);
                            byteBuf.writeBytes(Common.intToByte(1337));
                            byte[] control = new byte[byteBuf.readableBytes()];
                            byteBuf.readBytes(control);
                            RtmpResponse.sendData(control, MsgType.MSG_USER_CONTROL, 0, receive.receive, 0,receive.check_size);
                            receive.ready = true;
                            byteBuf.release();
                        }
                        if (!receive.keyframe) {
                            logger.info("关键进来了");
                            receive.keyframe = true;
                            if(timestamp < Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(publish.keyFrameMessage, MsgType.MSG_VIDEO, 1337, receive.receive, timestamp,receive.check_size);
                            } else {
                                int sendTime = timestamp - receive.prevTimestamp;
                                if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                    RtmpResponse.sendData(publish.keyFrameMessage, MsgType.MSG_VIDEO, 1337, receive.receive, timestamp,receive.check_size);
                                } else {
                                    RtmpResponse.sendDataExtTimestamp(publish.keyFrameMessage, MsgType.MSG_VIDEO, 1337, receive.receive, sendTime,receive.check_size);
                                }
                            }
                        }
                        if (receive.ready && receive.keyframe) {
                            if(timestamp < Common.TIMESTAMP_MAX_NUM){
                                RtmpResponse.sendData(message, MsgType.MSG_VIDEO, 1337, receive.receive, timestamp,receive.check_size);
                            } else {
                                int sendTime = timestamp - receive.prevTimestamp;
                                if(sendTime >= Common.TIMESTAMP_MAX_NUM){
                                    RtmpResponse.sendData(message, MsgType.MSG_VIDEO, 1337, receive.receive, timestamp,receive.check_size);
                                } else {
                                    RtmpResponse.sendDataExtTimestamp(message, MsgType.MSG_VIDEO, 1337, receive.receive, sendTime,receive.check_size);
                                }
                            }
                        }
                        if(timestamp >= Common.TIMESTAMP_MAX_NUM){
                            receive.prevTimestamp = timestamp;
                        }
                    }
                }
            }
        }catch (Exception e) {
            logger.info(e.getMessage());
        }
    }
}
